var lang;
$(document).ready(function(){
	lang = $("#display-lang").val();
	$(".tabLi").click(function(){
		clickTab($(this));
	});
});
function clickTab(tab){
	var old = $(".highlighted");
	if($(old).attr("id") != $(tab).attr("id")){
		$(old).removeClass("highlighted");
		$(old).addClass("faded");
		$(tab).removeClass("faded");
		$(tab).addClass("highlighted");
		if($(tab).attr("id")=="memberList"){
			$(".datasetList").hide();
			$(".relationList").hide();
			$(".viewList").hide();
			$(".entityList").hide();
			$(".memberList").show();
		}else if($(tab).attr("id")=="viewList"){
			if($(".viewList table tbody").children().length<=0){
				getViewList();
			}
			$(".datasetList").hide();
			$(".relationList").hide();
			$(".memberList").hide();
			$(".entityList").hide();
			$(".viewList").show();
		}else if($(tab).attr("id")=="entityList"){
			if($(".entityList table tbody").children().length<=0){
				getEntityList();
			}
			$(".datasetList").hide();
			$(".relationList").hide();
			$(".viewList").hide();
			$(".memberList").hide();
			$(".entityList").show();
		} else if($(tab).attr("id")=="datasetList"){
			if($(".datasetList table tbody").children().length<=0){
				getDatasetList();
			}
			$(".entityList").hide();
			$(".relationList").hide();
			$(".viewList").hide();
			$(".memberList").hide();
			$(".datasetList").show();
		} else if($(tab).attr("id")=="relationList"){
			if($(".relationList table tbody").children().length<=0){
				getRelationList();
			}
			$(".datasetList").hide();
			$(".entityList").hide();
			$(".viewList").hide();
			$(".memberList").hide();
			$(".relationList").show();
		}
	}
	
}
/*function getMemList(){
	var gname = $("#gname b").text();
	var data={};
	data["action"]="getMemList";
	data["gname"]=gname;
	data = JSON.stringify(data);
	data = ("msg="+encodeURIComponent(data));
	$.ajax({
		url:'groupDetailServlet',
		dataType:'text',
		type:'post',
		data:data,
		success:function(text){
			$(".viewList table tbody").append(text);
		}
	});
}*/
function getViewList(){
	$(".memberList").hide();
	var gname = $("#gname b").text();
	var data={};
	data["action"]="getViewList";
	data["gname"]=gname;
	data = JSON.stringify(data);
	data = ("msg="+encodeURIComponent(data));
	$.ajax({
		url:'/sview/groupDetailServlet',
		dataType:'text',
		type:'post',
		data:data,
		success:function(text){
			$(".viewList table tbody").append(text);
		}
	});
}
function getEntityList(){
	var gname = $("#gname b").text();
	var data={};
	data["action"]="getEntityList";
	data["gname"]=gname;
	data["lang"]=lang;
	data = JSON.stringify(data);
	data = ("msg="+encodeURIComponent(data));
	$.ajax({
		url:'/sview/groupDetailServlet',
		dataType:'text',
		type:'post',
		data:data,
		success:function(text){
			$(".entityList table tbody").append(text);
		}
	});
}
function getDatasetList(){
	var gname = $("#gname b").text();
	var data={};
	data["action"]="getDatasetList";
	data["gname"]=gname;
	data["lang"]=lang;
	data = JSON.stringify(data);
	data = ("msg="+encodeURIComponent(data));
	$.ajax({
		url:'/sview/groupDetailServlet',
		dataType:'text',
		type:'post',
		data:data,
		success:function(text){
			$(".datasetList table tbody").append(text);
		}
	});
}
function getRelationList(){
	var gname = $("#gname b").text();
	var data={};
	data["action"]="getRelationList";
	data["gname"]=gname;
	data = JSON.stringify(data);
	data = ("msg="+encodeURIComponent(data));
	$.ajax({
		url:'/sview/groupDetailServlet',
		dataType:'text',
		type:'post',
		data:data,
		success:function(text){
			$(".relationList table tbody").append(text);
		}
	});
}

function addMem(){
	var displayLang = $("#display-lang").val();
	var gid = $("#gid").val();
	var from = location.href;
	from = encodeURIComponent(from);
	
	if(displayLang=="en"){
		window.location = "/sview/group/addmem.jsp?gid="+gid+"&from="+from;
	}else if(displayLang=="zh"){
		window.location = "/sview/group/addmem_zh.jsp?gid="+gid+"&from="+from;
	}
}

function delMem(){
	var ch = $("table input:checked");
	var flag = true;
	var displayLang = $("#display-lang").val();
	if(ch.length>0){
		var names = "";
		ch.each(function(){
//			alert("|"+$(".uinfo").text().trim()+"|"+$(this).parent().next().text()+"|");
			if($(".uinfo").text().trim() == $(this).parent().next().text().trim()){
				if(displayLang == 'en'){
					alert("cannot delete yourself!");
				}else if(displayLang == 'zh'){
					alert("不能删除自己!");
				}
				
				flag = false;
				return ;
			}
			names += $(this).parent().next().text()+",";
		});
		if(flag){
			names = names.substring(0, names.length-1);
			var showText;
			if(displayLang == 'en'){
				showText = "Are you sure to delete member(s) "+names+"?";
			}else if(displayLang == 'zh'){
				showText = "是否确定要删除用户： "+names+"?";
			}
			if(confirm(showText)){
				var data={};
				data["action"]="delMem";
				data["gname"]=$("#gname b").text();
				data["unames"]=names;
				data["sender"]=$(".uinfo").text().trim();
				data = JSON.stringify(data);
				data = ("msg="+encodeURIComponent(data));
				$.ajax({
					url:'/sview/groupDetailServlet',
					dataType:'text',
					type:'post',
					data:data,
					success:function(text){
						var displayLang = $("#display-lang").val();
						if(displayLang == 'en'){
							alert("deleted!");
						}else if(displayLang == 'zh'){
							alert("已删除!");
						}
						
						window.location.reload();
					}
				});
			}
		}
	}else{
		if(displayLang == 'en'){
			alert("please select a member!");
		}else if(displayLang == 'zh'){
			alert("请选择要删除的成员!");
		}
		
	}
}